﻿using System.Windows.Input;
using YourWorld.Common;
using Microsoft.Practices.Composite.Presentation.Commands;
using MainGameModule.Models;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using YourWorld.Common.Infrastucture.Events;
using MainGameModule.Views;
using BusinessObjects;
using System.Collections.ObjectModel;

namespace MainGameModule.ViewModels
{
    public class PopUpTrashViewModel : ViewModelBase
    {
        private IRegionManager regionManager;
        private IUnityContainer container;
        private IGameModel gameModel;

        private Rubbish rubbish;
        private ICommand closeCommand;
        private ICommand takeNoActionCommand;
        private BinViewModel selectedBin;

        private ObservableCollection<BinViewModel> binItems;
        private bool isComplete = false;

        public PopUpTrashViewModel( IGameModel gameModel, IRegionManager regionManager, IUnityContainer container )
        {
            this.gameModel = gameModel;
            this.regionManager = regionManager;
            this.container = container;

            EventService.Aggregator.GetEvent<ChooseBinEvent>().Subscribe( this.BinSelected );
            EventService.Aggregator.GetEvent<AddToExperienceEventCompleted>().Subscribe( this.AddToExperienceEventCompleted );
        }

        public Rubbish Rubbish
        {
            get
            {
                return this.rubbish;
            }
            set
            {
                if ( this.rubbish == value )
                    return;

                this.isComplete = false;

                this.rubbish = value;
                this.NotifyPropertyChanged( "Rubbish" );
            }
        }

        public ICommand CloseCommand
        {
            get
            {
                if ( this.closeCommand == null )
                {
                    this.closeCommand = new DelegateCommand<string>( this.ExecuteClose );
                }
                return this.closeCommand;
            }
        }

        public ICommand TakeNoActionCommand
        {
            get
            {
                if ( this.takeNoActionCommand == null )
                {
                    this.takeNoActionCommand = new DelegateCommand<string>( this.ExecuteTakeNoActionCommand );
                }
                return this.takeNoActionCommand;
            }
        }

        public ObservableCollection<BinViewModel> BinItems
        {
            get
            {
                if ( this.binItems == null )
                    this.binItems = new ObservableCollection<BinViewModel>();

                return this.binItems;
            }
            set
            {
                if ( this.binItems == value )
                    return;

                this.binItems = value;
                this.NotifyPropertyChanged( "BinItems" );
            }
        }

        public BinViewModel SelectedBin
        {
            get
            {
                return this.selectedBin;
            }
            set
            {
                if ( this.selectedBin == value )
                    return;

                this.selectedBin = value;
                this.NotifyPropertyChanged( "SelectedBin" );
            }
        }

        public void BinSelected( int binId )
        {
            if ( binId == this.Rubbish.MaterialTypeId )
            {
                EventService.Aggregator.GetEvent<ChooseBinAnswerEvent>().Publish( true );
                PlayerStatusManager.Instance.AddExperience( Consts.PlayerRecycledRubbishExperencePoints );
                PlayerStatusManager.Instance.ContinueGame();

                // Right Answer:
                GameMainViewModel mainViewModel = this.container.Resolve<GameMainViewModel>();
                mainViewModel.RubbishFound( this.Rubbish );

                QuestsPreviewViewModel questsViewModel = this.container.Resolve<QuestsPreviewViewModel>();
                questsViewModel.RubbishFound( this.Rubbish );
            }
            else
            {
                EventService.Aggregator.GetEvent<ChooseBinAnswerEvent>().Publish( false );
            }
        }

        public void AddToExperienceEventCompleted( int i )
        {
            RubbishBinsRepository.Instance.CanIWinBadge();
        }

        private void ExecuteClose( string param )
        {
            if ( this.regionManager.Regions[RegionNames.GamePopUpRegion].Views.Contains( this.container.Resolve<PopUpTrashView>() ) )
                this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate( this.container.Resolve<PopUpTrashView>() );
        }

        private void ExecuteTakeNoActionCommand( string param )
        {
            PlayerStatusManager.Instance.AddExperience( Consts.PlayerTakeNoActionRubbishExperencePoints );
            PlayerStatusManager.Instance.ContinueGame();

            GameMainViewModel game = this.container.Resolve<GameMainViewModel>();
            this.gameModel.UpdateSeverBinsStatus( game.Player, this.Rubbish, RubbishRemovalStatus.Unlock );

            if ( this.regionManager.Regions[RegionNames.GamePopUpRegion].Views.Contains( this.container.Resolve<PopUpTrashView>() ) )
                this.regionManager.Regions[RegionNames.GamePopUpRegion].Deactivate( this.container.Resolve<PopUpTrashView>() );
        }
    }
}
